package com.yst.suivi.services.rights;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.naming.NamingException;

import com.yst.suivi.data.rights.Profil;
import com.yst.suivi.data.rights.ProfilUtil;
import com.yst.suivi.data.rights.Right;
import com.yst.suivi.data.user.User;
import com.yst.suivi.data.user.UserUtil;
import com.yst.suivi.services.AbstractSessionBean;

/**
 * @ejb.bean display-name="RightService" jndi-name="RightHomeRemote"
 *           name="RightView" type="Stateless" view-type="both"
 */
public class RightServiceBean extends AbstractSessionBean implements SessionBean
{
	/**
	 * @throws CreateException
	 * @ejb.create-method view-type = "both"
	 */
	public void ejbCreate() throws CreateException
	{
		// by default nothing to do
	}

	/**
	 * This method create a new user.
	 * 
	 * @param standard
	 * @return standard updated
	 * @throws CreateException
	 * @ejb.interface-method view-type = "both"
	 * @ejb.transaction type = "Required"
	 */
	public String createProfil(String nom, Collection lstRights, Collection lstUsers) throws NamingException, CreateException
	{
		Profil profil = ProfilUtil.getLocalHome().create();
		updateProfil(nom, lstRights, profil, lstUsers);
		return profil.getId();
	}

	/**
	 * La methode remplie l'objet EJB avec les valeurs fournies
	 * 
	 * @param nom
	 * @param prenom
	 * @param usr
	 * @throws NamingException
	 * @throws FinderException
	 * @ejb.interface-method view-type = "both"
	 */
	public void storeProfil(String idProfil, String nom, Collection lstRights, Collection lstUsers) throws FinderException, NamingException
	{
		Profil profil = ProfilUtil.getLocalHome().findByPrimaryKey(idProfil);
		updateProfil(nom, lstRights, profil, lstUsers);
	}

	/**
	 * @param nom
	 * @param lstRights
	 * @param profil
	 */
	private void updateProfil(String nom, Collection lstRights, Profil profil, Collection lstUsers)
	{
		profil.setNom(nom);
		profil.clearRight();
		profil.add(Right.REFRESH);
		Iterator it = lstRights.iterator();
		while (it.hasNext())
		{
			profil.add((Right) it.next());
		}
		profil.flushRight();
		profil.setUsers(lstUsers);
	}

	/**
	 * Retourne l'ensemble des profils de l'application.
	 * 
	 * @return
	 * @throws NamingException
	 * @throws FinderException
	 * @ejb.interface-method view-type = "both"
	 */
	public Collection findProfils() throws FinderException, NamingException
	{
		return ProfilUtil.getLocalHome().findAll();
	}

	/**
	 * Retourne l'ensemble des profils de l'application.
	 * 
	 * @return
	 * @throws NamingException
	 * @throws FinderException
	 * @ejb.interface-method view-type = "both"
	 */
	public Collection findUsersFor(Profil profil) throws FinderException, NamingException
	{
		Profil profil2 = ProfilUtil.getLocalHome().findByPrimaryKey(profil.getId());
		ArrayList lst = new ArrayList();
		Iterator it = profil2.getUsers().iterator();
		User usr;
		while (it.hasNext())
		{
			usr = (User) it.next();
			lst.add(UserUtil.getLocalHome().findByPrimaryKey(usr.getId()));
		}
		return lst;
	}

	/**
	 * La methode retourne vrai si l'utilisateur courant a le droit d'executer
	 * l'action associe au droit.
	 * 
	 * @param r
	 * @return
	 * @ejb.interface-method view-type = "both"
	 */
	public boolean canDo(Right r)
	{
		boolean retour = false;
		try
		{
			String login = getContext().getCallerPrincipal().getName();
			User usr = UserUtil.getLocalHome().findByLogin(login);
			Collection someProfil = ProfilUtil.getLocalHome().findForUser(usr);
			Iterator itProfil = someProfil.iterator();
			Enumeration enumRight;
			while (itProfil.hasNext())
			{
				enumRight = ((Profil) itProfil.next()).getRight();
				while (enumRight.hasMoreElements())
				{
					if (r.equals(enumRight.nextElement()))
					{
						retour = true;
					}
				}
			}
		}
		catch (FinderException e)
		{
			retour = false;
		}
		catch (NamingException e)
		{
			retour = false;
		}
		return retour;
	}
}
